package com.hoshiibuy.sys.server.api.impl;

import java.util.List;
import java.util.NoSuchElementException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.hoshiibuy.common.rpc.Result;
import com.hoshiibuy.sys.api.UserApi;
import com.hoshiibuy.sys.common.UserRequestDO;
import com.hoshiibuy.sys.common.UserResponseDO;
import com.hoshiibuy.sys.common.dto.request.ChangePasswordRequestDTO;
import com.hoshiibuy.sys.common.dto.request.GithubUserLoginRequestDTO;
import com.hoshiibuy.sys.common.dto.response.UserBaseInfoResponseDTO;
import com.hoshiibuy.sys.common.dto.response.UserInfoResponseDTO;
import com.hoshiibuy.sys.common.dto.response.UserSecurityQuestionsResponseDTO;
import com.hoshiibuy.sys.core.service.UserService;
import com.hoshiibuy.sys.core.service.model.user.UserBaseInfoModel;

@RestController
public class UserApiImpl implements UserApi {

	private Logger logger = LoggerFactory.getLogger(UserApiImpl.class);

	@Autowired
	private UserService userService;
	
	UserApi userApi;

	public Result<UserBaseInfoResponseDTO> getUserByName(String username) {
		UserBaseInfoResponseDTO userInfoModel = null;
		try {
			UserBaseInfoModel userEntity = userService.findByUserName(username);

			if (userEntity == null) {
				return Result.wrap404Error("user is not found");
			}

			userInfoModel = new UserBaseInfoResponseDTO();

			BeanUtils.copyProperties(userEntity, userInfoModel);

		} catch (Exception e) {
			return Result.wrap500Error(e.getMessage());
		}
		return new Result(userInfoModel);
	}

	public Result<UserInfoResponseDTO> getUserDetailByName(String username) {
		UserInfoResponseDTO userModel = null;
		try {
			userModel = userService.getUserDetailByNameAndServerName(username, null);
			if (userModel == null) {
				return Result.wrap404Error("user is not found");
			}
		} catch (Exception e) {
			return Result.wrap500Error(e.getMessage());
		}

		return new Result(userModel);
	}

	public Result<UserInfoResponseDTO> getUserDetailByNameAndServerName(String username, String serverName) {
		UserInfoResponseDTO userModel = null;
		try {
			userModel = userService.getUserDetailByNameAndServerName(username, serverName);
			if (userModel == null) {
				return Result.wrap404Error("user is not found");
			}
		} catch (Exception e) {
			return Result.wrap500Error(e.getMessage());
		}
		return new Result(userModel);
	}

	public Result<List<UserSecurityQuestionsResponseDTO>> getUserSecurityQuestions(String username) {
		List<UserSecurityQuestionsResponseDTO> responseDTOList = null;
		try {
			responseDTOList = userService.findUserSecurityQuestions(username);
			if (responseDTOList == null) {
				return Result.wrap404Error("securityQuestions is not found");
			}
		} catch (Exception e) {
			logger.warn("getUserSecurityQuestions error.", e);
			return Result.wrap500Error(e.getMessage());
		}
		return new Result(responseDTOList);
	}

	@Override
	public Result<Void> changePassword(@RequestBody ChangePasswordRequestDTO changePasswordRequestDTO) {
		try {
			userService.modifyPassword(changePasswordRequestDTO);
		} catch (Exception e) {
			logger.warn("modify password error.", e);
			return Result.wrap500Error(e.getMessage());
		}
		return new Result();
	}

	@Override
	public Result<Void> thirdPartyUserLogin(@RequestBody GithubUserLoginRequestDTO githubUserLoginRequestDTO) {
		try {
			userService.thirdPartyLogin(githubUserLoginRequestDTO);
		} catch (Exception e) {
			logger.warn("third party login error.", e);
			return Result.wrap500Error(e.getMessage());
		}
		return new Result();
	}

	@Override
	public Result<UserResponseDO> commonRequest(@RequestBody UserRequestDO userRequestDO) {
		UserResponseDO userResponseDO = null;
		try {
			userResponseDO = userService.commonRequest(userRequestDO);
			if (userResponseDO == null) {
				return Result.wrap404Error("user is not found");
			}
		} catch (NoSuchElementException e) {
			return Result.wrap404Error("user is not found");
		} catch (Exception e) {
			return Result.wrap500Error(e.getMessage());
		}
		return new Result(userResponseDO);
	}
}
